﻿using System;
using System.Data;
using vtech_formBase;
using vtech_frameWorks.Exceptions;
using vtech_frameWorks.Common;

namespace vtech_system.Department
{
    public partial class frmAddDepartment : frmAdd
    {
        #region Delegate
        public delegate void AddDelegate();
        public event AddDelegate OnAddDelegate = null;

        protected override void FireEvent()
        {
            if (OnAddDelegate != null)
                OnAddDelegate();
        }
        #endregion
        
        public frmAddDepartment()
        {
            InitializeComponent();
        }

        protected override bool CheckControl()
        {
            try
            {
                errorProvider.Dispose();
                if (txtDepartmentCode.Text == "")
                {
                    errorProvider.SetError(txtDepartmentCode, "Bạn chưa nhập mã phòng ban. Xin vui lòng kiểm tra lại");
                    return false;
                }
                else
                {
                    if (txtDepartmentName.Text == "")
                    {
                        errorProvider.SetError(txtDepartmentName, "Bạn chưa nhập tên văn phòng. Xin vui lòng kiểm tra lại");
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                return false;
            }
        }

        protected override void Insert()
        {
            try
            {
                using (DsDepartment dsDepartment = new DsDepartment())
                {
                    DataRow drow = dsDepartment.DtDepartment.NewRow();
                    drow["DepartmentID"] = Guid.NewGuid();
                    drow["DepartmentCode"] = txtDepartmentCode.Text.Trim();
                    drow["DepartmentName"] = txtDepartmentName.Text.Trim();
                    drow["BranchID"] = ClsGlobal.BranchID;
                    drow["Description"] = txtDescription.Text.Trim();
                    drow["Inactive"] = 0;
                    dsDepartment.DtDepartment.Rows.Add(drow);
                    ClsDepartmentBS.DoInsert(dsDepartment.DtDepartment);
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }
    }
}
